package com.ft.oa.service.dev.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dev.DevProblem;
import com.ft.oa.domain.dev.Milestone;
import com.ft.oa.domain.development.DevAllocate;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.enums.ManagerEnum;
import com.ft.oa.mapper.dev.MilestoneMapper;
import com.ft.oa.mapper.dev.MilestoneMapper;
import com.ft.oa.mapper.development.DevAllocateMapper;
import com.ft.oa.service.dev.MilestoneService;
import com.ft.oa.service.dev.MilestoneService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * Author: yihui
 * Date: 2022/1/21 10:15 AM
 * FileName: MilestoneServiceImpl
 * Description: 研发里程碑
 */
@Transactional
@Service
public class MilestoneServiceImpl implements MilestoneService {

    @Resource
    MilestoneMapper milestoneMapper;
    @Resource
    DevAllocateMapper devAllocateMapper;

    @Override
    public ApiResult add(Milestone milestone) {
        Long devAllocateId = milestone.getDevAllocateId();
        DevAllocate devAllocate = devAllocateMapper.selectById(devAllocateId);
        //审核通过的项目才能创建里程碑 审核状态 0-审核中  1-通过  2-驳回
        if(  1 != devAllocate.getApprovalStatus()){
            return ApiResult.builder().code(30002).msg("审批未通过，无法创建里程碑").build();
        }
        Long createStaffId = SecurityUtils.getCurrentUser().getId();
        milestone.setCreateStaffId(createStaffId);
        milestone.setCreateDate(LocalDate.now());
        milestone.setCreateDateTime(LocalDateTime.now());
        milestoneMapper.insert(milestone);
        return ApiResult.simpleSuccess();
    }

    @Override
    public ApiResult edit(Milestone milestone) {
        milestone.setUpdateDateTime(LocalDateTime.now());
        milestoneMapper.updateById(milestone);
        return ApiResult.simpleSuccess();
    }

    @Override
    public ApiResult del(Milestone milestone) {
        LambdaUpdateChainWrapper<Milestone> wrapper = new LambdaUpdateChainWrapper<>(milestoneMapper);
        boolean update = wrapper.eq(Milestone::getId, milestone.getId())
                .set(Milestone::getDel, true)
                .set(Milestone::getDelTime, LocalDateTime.now())
                .update();
        return ApiResult.simpleSuccess();
    }

    @Override
    public Page<Milestone> page(Milestone condition) {
        Page<Milestone> pages = new Page<>(condition.getPage(), condition.getSize());
        QueryWrapper<Milestone> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Milestone::getDevAllocateId,condition.getDevAllocateId())
                .eq(Milestone::getDel,false)
                .eq(ManagerEnum.GENERAL.toString().equals(SecurityUtils.getCurrentUser().getManager()), Milestone::getCreateStaffId,SecurityUtils.getCurrentUser().getId())
                .ge(StringUtils.hasText(condition.getCreateStartDate()),Milestone::getCreateDate,condition.getCreateStartDate())
                .le(StringUtils.hasText(condition.getCreateEndDate()),Milestone::getCreateDate,condition.getCreateEndDate())
                .ge(StringUtils.hasText(condition.getPlanFinishStartDate()),Milestone::getPlanFinishDate,condition.getPlanFinishStartDate())
                .le(StringUtils.hasText(condition.getPlanFinishEndDate()),Milestone::getPlanFinishDate,condition.getPlanFinishEndDate())
                .ge(StringUtils.hasText(condition.getActualFinishStartDate()),Milestone::getActualFinishDate,condition.getActualFinishStartDate())
                .le(StringUtils.hasText(condition.getActualFinishEndDate()),Milestone::getActualFinishDate,condition.getActualFinishEndDate())
                .eq(null != condition.getDevAllocateId() ,Milestone::getDevAllocateId, condition.getDevAllocateId())
                .like(StringUtils.hasText(condition.getPrincipalStaffName()) , Milestone::getPrincipalStaffName, condition.getPrincipalStaffName());
        Page<Milestone> result = milestoneMapper.selectPage(pages, wrapper);
        return result;
    }
}
